package view;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import cliente.ClienteImp;
import javax.swing.JScrollPane;

import dominio.bean.DetalleLiquidacion;
import dominio.bean.Liquidacion;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashSet;
import javax.swing.ScrollPaneConstants;
import javax.swing.JList;

public class PaDetalleDeLiquidacion extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField txtCodigoLiquidacion;

	/**
	 * Create the panel.
	 * @param cliente 
	 */
	public PaDetalleDeLiquidacion(final ClienteImp cliente) {
		
		JLabel lblDetallesDeLiquidacion = new JLabel("Detalles de Liquidacion");
		lblDetallesDeLiquidacion.setHorizontalAlignment(SwingConstants.LEFT);
		lblDetallesDeLiquidacion.setForeground(Color.BLUE);
		lblDetallesDeLiquidacion.setFont(new Font("Sylfaen", Font.BOLD | Font.ITALIC, 18));
		
		JButton btnBuscarLiquidacion = new JButton("Buscar Liquidacion");


		
		JButton button_1 = new JButton("Cancelar");
		
		JSeparator separator = new JSeparator();
		
		JLabel lblCodigoLiquidacion = new JLabel("Codigo Liquidacion");
		
		txtCodigoLiquidacion = new JTextField();
		txtCodigoLiquidacion.setColumns(10);
		
		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblDetallesDeLiquidacion, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
									.addGap(4)
									.addComponent(btnBuscarLiquidacion, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
									.addGap(6)
									.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(10)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 420, GroupLayout.PREFERRED_SIZE)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblCodigoLiquidacion, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(txtCodigoLiquidacion, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)))))
							.addGap(20))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(separator, GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
							.addContainerGap())))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblDetallesDeLiquidacion, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(1)
							.addComponent(btnBuscarLiquidacion))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(1)
							.addComponent(button_1)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(separator, GroupLayout.PREFERRED_SIZE, 4, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(txtCodigoLiquidacion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(3)
							.addComponent(lblCodigoLiquidacion)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
					.addContainerGap())
		);
		final DefaultListModel listModel = new DefaultListModel();		
	
		btnBuscarLiquidacion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				HashSet<DetalleLiquidacion> detalles = cliente.obtenerDetallesLiquidacion(Integer.parseInt(txtCodigoLiquidacion.getText()));
				String root = new String();
				Liquidacion liq = cliente.obtenerLiquidacion(Integer.parseInt(txtCodigoLiquidacion.getText()));
				
				if (liq!=null){
					listModel.clear();
					root = "Empresa Transporte: " + liq.getEmpresaTransporte().getNombre();
					listModel.addElement(root);
					listModel.addElement("");
					listModel.addElement("");
					for(DetalleLiquidacion det : detalles)
					{
						String detalle = new String ();
						detalle = "id Formulario Compra: " + String.valueOf(det.getFormularioCompraProductos().getIdFormulario().getIdFormularioCompra()) 
								+ " id Item: "+ String.valueOf(det.getFormularioCompraProductos().getIdFormulario().getIdItem())
								+ " id Producto: " + String.valueOf(det.getFormularioCompraProductos().getProducto().getIdProducto()) +
								" Codigo Postal: " + det.getFormularioCompraProductos().getCodigoPostal().getIdCodPostal().getIdCodigoPostal() +
								" Producto: "+ det.getFormularioCompraProductos().getProducto().getDescripcion()+
								" Precio: " + String.valueOf(det.getPrecio());
						listModel.addElement(detalle);								
					}
				}
				else{
					listModel.clear();
					JOptionPane.showMessageDialog(null, "No existe el numero de la liquidacion");
				}
			}
		});
		JList list = new JList(listModel);
		scrollPane.setViewportView(list);
		setLayout(groupLayout);
		

	}
}
